package org.hzero.order.app.service.impl;

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.hzero.boot.imported.app.service.IDoImportService;
import org.hzero.boot.imported.infra.validator.annotation.ImportService;
import org.hzero.order.domain.entity.dto.OrderDTO;
import org.hzero.order.domain.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author xiuiqng.Yang-Estelle-양수청
 * @date 2019/8/6 16:00
 */

@ImportService(templateCode = "IMPORT-TEMPLATE")
public class ImportServiceImpl implements IDoImportService {

    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private OrderRepository orderRepository;

    @Override
    public Boolean doImport(String data) {
        OrderDTO order;
        try {
            order = objectMapper.readValue(data, OrderDTO.class);
        } catch (IOException e) {
            // 失败
            return false;
        }
        orderRepository.insertSelective(order);
        // 成功
        return true;
    }
}
